import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { Window } from '@kit.ArkUI';
import StorageUtil from '../utils/StorageUtil';

export default class MainAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onWindowStageCreate(windowStage: Window.WindowStage): void {
    // 设置主窗口
    windowStage.loadContent('pages/LoginPage', async (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
        return;
      }

      // 检查用户是否已登录
      const isLoggedIn = await StorageUtil.get('isLoggedIn');
      if (isLoggedIn) {
        // 已登录则跳转到主页面
        this.context.navigateToPage({
          uri: 'pages/Index'
        });
      }
    });
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }
}